package cn.b.training.baby_education_training.repository;

import cn.b.training.baby_education_training.entity.UserInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * @program: baby_education
 * @description
 * @author: wuying
 * @create: 2022-11-29 10:26
 **/
@Repository
public interface UserInfoRepository extends JpaRepository<UserInfo,Long> {
    /**
     * 通过手机号获取用户信息
     * @param phone
     * @return
     */
    @Query("select a from UserInfo a where a.phone=?1")
    UserInfo getByPhone(String phone);

    //@Query("select a from UserInfo a where a.username=?1")
    //UserInfo findByUserName(String login);


    /**
     * 通过id获取用户信息
     * @param id
     * @return
     */
    @Query("select a from UserInfo a where a.id=?1")
    UserInfo getByUserId(Long id);

    /**
     * 修改用户信息
     * @param phone
     * @return
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Modifying
    @Query(value = "update user_info set phone=?2 where id=?1",nativeQuery = true)
    int updatePhone(String phone,Long id);

    /**
     *更改用户状态
     * @param id 用户id
     * @param status 用户更新的状态
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Modifying
    @Query(value = "update user_info set status=?2 where id=?1",nativeQuery = true)
    void changeStatus(@Param("id") Long id, @Param("status") String status);



    ///**
    // * 修改用户昵称
    // * @param nickname
    // * @return
    // */
    //@Transactional
    //@Modifying
    //@Query("update UserInfo set nickname=?1 where id=?2")
    //int updateNickname(String nickname);


}
